//
//  CustomView.m
//  绘图
//
//  Created by MyMac on 15/6/12.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//
#define CustomColor(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define RandomColor CustomColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
#import "CustomView.h"
@interface CustomView()
@property(nonatomic,assign)NSInteger snowY;
@property(nonatomic,strong)CADisplayLink*link;
@end
@implementation CustomView

-(void)awakeFromNib
{
//    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];
//    self.link=link;
//    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

}
- (void)drawRect:(CGRect)rect {
    //[self path];
   // [self randui:rect];
    [self Arc];
}
-(void)shan
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //设置扇形填充色
    CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor redColor] CGColor]));
    //设置圆心
    CGContextMoveToPoint(ctx, 100, 100);
    //以90为半径围绕圆心画指定角度扇形，0表示逆时针
    CGContextAddArc(ctx, 100, 100, 90,M_PI_2,M_PI, 0);
    //CGContextClosePath(ctx);
    CGContextFillPath(ctx);
}
-(void)randui:(CGRect)rect
{
    // 1.获得上下文
    
    // 2.画圆弧
    // x\y : 圆心
    // radius : 半径
    // startAngle : 开始角度
    // endAngle : 结束角度
    // clockwise : 圆弧的伸展方向(0:顺时针, 1:逆时针)
    
  //  int i=0;
 
    
    // 3.显示所绘制的东西
  
//     CGContextRef ctf=UIGraphicsGetCurrentContext();
//    CGContextAddEllipseInRect(ctf, CGRectMake(100, 100, 100, 100));
//  
//       CGContextSetStrokeColorWithColor(ctf, [UIColor redColor].CGColor);
   // UIGraphicsBeginImageContext(self.bounds.size);
    CGContextRef ctx=UIGraphicsGetCurrentContext();
  
    CGContextMoveToPoint(ctx, 100, 100);
    CGContextAddLineToPoint(ctx, 0, 100);
    CGContextAddLineToPoint(ctx, 0, rect.size.height);
    CGContextAddLineToPoint(ctx, rect.size.width-100, rect.size.height);
    CGContextAddLineToPoint(ctx, rect.size.width, rect.size.height-100);
    CGContextAddLineToPoint(ctx, rect.size.width, 0);
    CGContextClosePath(ctx);
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
   // CGContextStrokePath(ctx);
  //  [self.image drawInRect:rect];
    CGContextClip(ctx);
   // CGContextFillPath(ctx);
    UIImage*image=[UIImage imageNamed:@"屏幕快照 2015-06-15 下午6.11.53.png"];
  
   [image drawInRect:CGRectMake(50, 50, 100, 100)];
    CGContextStrokePath(ctx);
    

}
-(void)imagePoint:(CGRect)rect
{
    self.snowY+=1;
    
    if (self.snowY >= rect.size.height) {
        self.snowY =100;
    }
    
    UIImage *image = [UIImage imageNamed:@"屏幕快照 2015-06-12 下午3.01.30.png"];
    [image drawAtPoint:CGPointMake(10, self.snowY)];//照片的起点在哪

}
-(void)path
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 1.先创建一个路径
    CGMutablePathRef linePath = CGPathCreateMutable();
    
    
    
    // 2.拼接路径
    CGPathMoveToPoint(linePath, NULL, 0, 0);
    CGPathAddLineToPoint(linePath, NULL, 100, 100);
    
    // 3.添加路径到上下文
    CGContextAddPath(ctx, linePath);
    
    CGMutablePathRef circlePath = CGPathCreateMutable();
    CGPathAddArc(circlePath, NULL, 150, 150, 50, 0, M_PI * 2, 0);
    CGContextAddPath(ctx, circlePath);
    
    // 4.渲染
    CGContextStrokePath(ctx);
    
    
    CGPathRelease(linePath);
    CGPathRelease(circlePath);
    
    
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
    CGColorSpaceRelease(cs);

}
-(void)Arc
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
     CGFloat d=M_PI/20;
    for(NSInteger i=0;i<20;i++){
        CGContextMoveToPoint(ctx, 100, 100);
        CGContextSetFillColorWithColor(ctx, [RandomColor CGColor]);
          CGContextSetStrokeColorWithColor(ctx, [UIColor clearColor].CGColor);
        CGContextAddArc(ctx, 100, 100, 100, d*i, d*(i+1), 0);
        NSLog(@"%@",RandomColor);
        CGContextFillPath(ctx);
        CGContextMoveToPoint(ctx, 100, 100);
        CGContextSetFillColorWithColor(ctx, [[UIColor whiteColor] CGColor]);
        CGContextAddArc(ctx, 100, 100, 80,  d*i, d*(i+1), 0);
        
        
        CGContextFillPath(ctx);
        
    }


}
@end
